//
//  DiagnosisCameraViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-12.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "DiagnosisCameraViewController.h"
#import "SelfDiagnosisViewModel.h"
#import "DiagnosisCameraViewModel.h"

#import <QuartzCore/QuartzCore.h>
#import <QuartzCore/CoreAnimation.h>

#import <AssetsLibrary/AssetsLibrary.h>

#define kDelayInSeconds 2.0

@interface DiagnosisCameraViewController ()

@property (strong, nonatomic) IBOutlet UIButton *resultOkButton;
@property (strong, nonatomic) IBOutlet UIButton *resultNgButton;
@property (strong, nonatomic) IBOutlet UIButton *resultNoSupportButton;
@property (strong, nonatomic) IBOutlet UILabel *headerLabel;

@end

@implementation DiagnosisCameraViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.resultOkButton.enabled = NO;
    self.resultNgButton.enabled = NO;
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    self.viewModel = [[DiagnosisCameraViewModel alloc] init];
    
    void (^resultBlock)(BOOL) = ^(BOOL result) {
        NSLog(@"Camera is OK : %@", result ? @"YES" : @"NO");
        [self performSegueWithIdentifier:@"CameraCompleted" sender:self];
    };
    self.resultOkButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock simple:YES];
    self.resultNgButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock simple:NO];
    self.resultNoSupportButton.rac_command = [self.viewModel makeCommandWithBlock:resultBlock detail:DiagnosisCheckResultNoSupport];
    
    RAC(self, resultNgButton.hidden) = [RACObserve(self, hasCameraDevice) not];
    RAC(self, resultOkButton.hidden) = RACObserve(self, resultNgButton.hidden);
    RAC(self, resultNoSupportButton.hidden) = [RACObserve(self, resultNgButton.hidden) not];
    
    RAC(self, headerLabel.text) = RACObserve(self, viewModel.headerString);
}

- (void)handleSingleTap:(id)x
{
    NSLog(@"Touch begining");
    if (!self.presentedViewController) {
        [self startCheck];
    }
}

#pragma mark - camera

- (BOOL)hasCameraDevice
{
    BOOL isRearSupport = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    BOOL isFrontSupport = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
    
    return isRearSupport || isFrontSupport;
}

- (void)startCheck
{
    DiagnosisCameraViewModel *camVM = (DiagnosisCameraViewModel *)self.viewModel;
    BOOL isFrontSupport = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
    
    MCCameraDevice cameraDevice = MCCameraDeviceUnknown;
    switch (camVM.checkingCameraDevice) {
        case MCCameraDeviceUnknown:
            cameraDevice = MCCameraDeviceRear;
            break;
            
        case MCCameraDeviceRear:
            if (isFrontSupport) {
                cameraDevice = MCCameraDeviceFront;
            } else {
                cameraDevice = MCCameraDeviceUnknown;
            }
            break;
            
        case MCCameraDeviceFront:
            cameraDevice = MCCameraDeviceUnknown;
            break;
            
        default:
            break;
    }
    
    @weakify(self);
    [[camVM openCameraDevice:cameraDevice] subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[UIViewController class]]) {
            [self presentViewController:(UIViewController *)x animated:YES completion:NULL];
            self.resultNgButton.enabled = YES;
            
        } else if ([x isKindOfClass:[UIView class]]) {
            [self showFullscreenModalViewWithView:(UIView *)x animated:NO completion:^{
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kDelayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self dismissFullscreenModalViewWithView:(UIView *)x animated:(cameraDevice == MCCameraDeviceUnknown) completion:^{
                        [self startCheck];
                    }];
                });
            }];
            
            self.resultOkButton.enabled = YES;
        }
        
    } error:^(NSError *error) {
        @strongify(self);
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"エラー情報"
                                                        message:@"このデバイスが写真撮影できません。"
                                                       delegate:nil cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        
        self.resultNgButton.enabled = YES;
        
    }];
    
}

@end
